package tv.scolopender.listener;

import java.util.ArrayList;

import tv.scolopender.model.Scolo;
import android.app.Activity;
import android.os.AsyncTask;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

public class ScoloListScrollListener implements OnScrollListener {
	private final ArrayList<Scolo> scolos;
	private int totalScolos;
	private final Activity activity;
	private final AsyncTask<Integer, ?, ?> asyncTask;

	public ScoloListScrollListener(ArrayList<Scolo> scolos, int totalScolos,
			Activity activity, AsyncTask<Integer, ?, ?> asyncTask) {
		super();
		this.scolos = scolos;
		this.totalScolos = totalScolos;
		this.activity = activity;
		this.asyncTask = asyncTask;
	}

	public ScoloListScrollListener(ArrayList<Scolo> scolos, Activity activity,
			AsyncTask<Integer, ?, ?> asyncTask) {
		this(scolos, 0, activity, asyncTask);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// // Check if the last view is visible
		if (++firstVisibleItem + visibleItemCount > totalItemCount
				&& totalScolos < totalItemCount) {
			activity.setProgressBarIndeterminateVisibility(true);
			totalScolos = totalItemCount;

			asyncTask.execute(totalScolos, 5);
			// if (moreNewest.getStatus() !=
			// AsyncTask.Status.RUNNING) {
			// if (getTitle().equals(NEWS)) {
			// new ReadNewest().execute(totalItemCount, 3);
			// } else if (getTitle().equals(BEST)) {
			// new ReadBest().execute(totalItemCount, 3);
			// } else if (getTitle().equals(LIKES)) {
			// new ReadLikes().execute(totalItemCount, 3);
			// }
		}
	}

}
